//
//  LJP_SearchResultViewController.m
//  God_is_a_Girl
//
//  Created by 李继鹏 on 15/9/29.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LJP_SearchResultViewController.h"
#import <AFNetworking.h>
#import "LJP_searchRecomModel.h"
#import "LJP_SearchTripsModel.h"
#import "LJP_SearchPlaceCell.h"
#import <UIImageView+WebCache.h>
#import "LHQDestinationDetailViewController.h"
#import "LJP_ DetailsViewController.h"

@interface LJP_SearchResultViewController ()<UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *placeArray;
@property (nonatomic, strong) NSMutableArray *travelArray;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation LJP_SearchResultViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    
    return UIStatusBarStyleLightContent;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.placeArray = [NSMutableArray array];
    self.travelArray = [NSMutableArray array];
    self.searchHeader = [[LJP_SearchHeader alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.searchHeader.backgroundColor = self.mainColor;
    [self.view addSubview:self.searchHeader];
    [self.searchHeader.cancle addTarget:self action:@selector(cancleClicked) forControlEvents:UIControlEventTouchUpInside];
    self.searchHeader.searchBar.delegate = self;
    self.searchHeader.searchBar.text = self.searchText;
    [self dataHandle];
    [self tableViewSubview];
}
- (void)dataHandle{
    [self.placeArray removeAllObjects];
    [self.travelArray removeAllObjects];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *searchString = [self.searchHeader.searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *stringPath = [NSString stringWithFormat:@"http://api.breadtrip.com/search/?key=%@&start=0&count=20", searchString];
    NSLog(@"%@", stringPath);
    [manager GET:stringPath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *placeArray = [responseObject objectForKey:@"places"];
        NSArray *travelArray = [responseObject objectForKey:@"trips"];
        for (NSDictionary *dic in placeArray) {
            LJP_searchRecomModel *model = [[LJP_searchRecomModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [model setValue:[dic objectForKey:@"id"] forKeyPath:@"nextId"];
            [self.placeArray addObject:model];
        }
        for (NSDictionary *dic in travelArray) {
            LJP_SearchTripsModel *model = [[LJP_SearchTripsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [model setValue:dic[@"id"] forKey:@"traceid"];
            [self.travelArray addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
- (void)tableViewSubview
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:0];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%ld", section);
    if (section == 0) {
        NSLog(@"%ld", self.placeArray.count);
        return self.placeArray.count;
        
    } else {
        
        return self.travelArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"place"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"place"];
        }
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[self.placeArray[indexPath.row] icon]] placeholderImage:[UIImage imageNamed:@"search"]];
        cell.textLabel.text = [self.placeArray[indexPath.row] name];
        return cell;
    } else {
        LJP_SearchPlaceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"trips"];
        if (cell == nil) {
            cell = [[LJP_SearchPlaceCell alloc] initWithStyle:0 reuseIdentifier:@"trips"];
        }
        cell.title.textColor = self.mainColor;
        cell.model = self.travelArray[indexPath.row];
        return cell;
    }
}
- (void)cancleClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchHeader.searchBar resignFirstResponder];
    [self dataHandle];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 60;
    } else {
        return 95;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
       
        LHQDestinationDetailViewController *destination = [[LHQDestinationDetailViewController alloc] init];
        LJP_searchRecomModel *model = self.placeArray[indexPath.row];
        destination.type = model.type;
        destination.detailStr = model.nextId;
        [self.navigationController pushViewController:destination animated:YES];
    } else {
        LJP__DetailsViewController *details = [[LJP__DetailsViewController alloc] init];
        LJP_SearchTripsModel *model = self.travelArray[indexPath.row];
        details.stringId = model.traceid.stringValue;
        [self.navigationController pushViewController:details animated:YES];
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
